//
//  FLLClassifyViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/1.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLClassifyViewController.h"
#import "ZWYNetTool.h"
#import "FLLClassifyModel.h"
#import "ClassifyData.h"
#import "ClassifySecData.h"
#import "ClassifyThirdData.h"
#import "FLLMacro.h"
#import "FLLClassifyTableViewCell.h"
#import "FLLClassifyHeaderView.h"
#import "FLLLikeViewController.h"

@interface FLLClassifyViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) FLLClassifyModel *fllClassifyModel;
@property (nonatomic, strong) UITableView *classifyTableView;
@property (nonatomic, strong) FLLClassifyHeaderView *headerView;
@property (nonatomic, strong) NSArray<ClassifySecData *> *sectionArray;
/* 每个分区item个数 */
@property (nonatomic, assign) NSInteger itemTotalCount;
@property (nonatomic, strong) NSArray<ClassifyThirdData *> *itemArray;
@end

@implementation FLLClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createPOSTRequest];
    [self createClassifyTableView];
}
- (void)createPOSTRequest
{
    
    [ZWYNetTool POST:@"http://api.izhangchu.com/?appVersion=4.5&sysVersion=9.0.2&devModel=iPhone" andBody:@"methodName=CategoryIndex&version=4.40&" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
    
        self.fllClassifyModel = [FLLClassifyModel modelWithDic:result];

        
        // 老师写的  还没仔细看!!!!!!!!!!!!
        // 重新整理数据 把图片和label整理到一个数组里
        self.sectionArray = self.fllClassifyModel.data.data;
        for (int i = 0; i < _sectionArray.count; i++) {
            
            NSMutableArray<ClassifyThirdData *> *array = [NSMutableArray arrayWithArray:[_sectionArray[i] data]];
            NSString *imageName = [_sectionArray[i] image];
            ClassifyThirdData *imageData = [[ClassifyThirdData alloc] init];
            imageData.image = imageName;
            [array insertObject:imageData atIndex:0];
            _sectionArray[i].data = array;
        }
        [self.classifyTableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];

    
}
- (void)createClassifyTableView
{

    self.classifyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) style:UITableViewStylePlain];
    [self.view addSubview:_classifyTableView];
    _classifyTableView.delegate = self;
    _classifyTableView.dataSource = self;
    [_classifyTableView registerClass:[FLLClassifyTableViewCell class] forCellReuseIdentifier:@"classifyTableCell"];
    [_classifyTableView registerClass:[FLLClassifyHeaderView class] forHeaderFooterViewReuseIdentifier:@"classifyHeaderCell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return _sectionArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FLLClassifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"classifyTableCell"];
    
    // label的内容
    self.itemArray = self.fllClassifyModel.data.data[indexPath.section].data;
    cell.itemArray = _itemArray;
    
    // 自适应cell高度获取的item个数
    _itemTotalCount = self.fllClassifyModel.data.data[indexPath.section].data.count;
    
    __weak FLLClassifyViewController *weakSelf = self;
    cell.pushBlock = ^(NSString *idString, NSString *titleString){
    
        FLLLikeViewController *likeVC = [[FLLLikeViewController alloc] init];
        [weakSelf.navigationController pushViewController:likeVC animated:YES];
        likeVC.idString = idString;
        likeVC.titleString = titleString;
    };
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat itemWidth = (SCREEN_WIDTH - 6 * CellMargin_10) / 5.0;
    CGFloat itemHeight = itemWidth / 1.5;
    if (_itemTotalCount > 7) {
        if ((_itemTotalCount - 7) % 5 == 0) {
            return ((((_itemTotalCount - 7) / 5 ) + 2) * itemHeight + (((_itemTotalCount - 7) / 5 + 1) + 1) * CellMargin_10) ;
        }
        return ((((_itemTotalCount - 7) / 5 + 1) + 2) * itemHeight + (((_itemTotalCount - 7) / 5 + 1) + 1) * CellMargin_10) ;
    }
    return (2 * itemHeight + 3 * CellMargin_10);
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    self.headerView = [[FLLClassifyHeaderView alloc] initWithReuseIdentifier:@"classifyHeaderCell"];
    _headerView.sectionLabel.text = _sectionArray[section].text;
    _headerView.sectionLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
    return _headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 60 * kHeightScale;
}
// 改变分区头背景颜色
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    
    view.tintColor = [UIColor whiteColor];
}
// 取消分区头悬浮效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = 60 * kHeightScale;
    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(- scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(- sectionHeaderHeight, 0, 0, 0);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
